﻿using Cocon90.Lib.Dal.Error;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cocon90.Lib.Dal
{
    public class DataHelperFactory
    {
        /// <summary>
        /// 取得数据库连接实例 配置文件中的connectionStrings中，添加name="ConnectionString"providerName="Cocon90.Lib.Dal.Utility.SQLDataHelper"  connectionString="server=.;database=Cocon90_OA;uid=sa;pwd=123456;";需要注意的是，如果给连接字符串的Name指定了其它值，则会影响其它类如ModelHelper的调用。
        /// </summary>
        /// <returns></returns>
        public static IDataHelper CreateInstence(string connectionStringName = "ConnectionString")
        {
            string classFullNamespace = System.Configuration.ConfigurationManager.ConnectionStrings[connectionStringName].ProviderName;
            if (!classFullNamespace.Contains(".")) throw new InstenceException("ConnectionString的ProviderName不正确，必须包含完整的类名路径");
            object result = Activator.CreateInstance(Type.GetType(classFullNamespace));
            return (IDataHelper)result;
        }

    }
}
